// pages/index/index.js
import { httpBlock } from '../../utils/util'

let app = getApp();
const globalData = app.globalData;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    subject: {
      movie_showing: {},
      movie_hot_gaia: {},
      tv_variety_show: {},
      tv_hot: {},
      book_bestseller: {},
      music_single: {}
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const movie_showing = `${globalData.g_requestApi}subject_collection/movie_showing/items?start=0&count=20&apiKey=054022eaeae0b00e0fc068c0c0a2102a`
    const movie_hot_gaia = `${globalData.g_requestApi}subject_collection/movie_hot_gaia/items?start=0&count=20&apiKey=054022eaeae0b00e0fc068c0c0a2102a`
    const tv_variety_show = `${globalData.g_requestApi}subject_collection/tv_variety_show/items?start=0&count=20&apiKey=054022eaeae0b00e0fc068c0c0a2102a`
    const tv_hot = `${globalData.g_requestApi}subject_collection/tv_hot/items?start=0&count=20&apiKey=054022eaeae0b00e0fc068c0c0a2102a`
    const book_bestseller = `${globalData.g_requestApi}subject_collection/book_bestseller/items?start=0&count=20&apiKey=054022eaeae0b00e0fc068c0c0a2102a`
    const music_single = `${globalData.g_requestApi}subject_collection/music_single/items?start=0&count=20&apiKey=054022eaeae0b00e0fc068c0c0a2102a`

    httpBlock(movie_showing, 'movie_showing', this.processData)
    httpBlock(movie_hot_gaia, 'movie_hot_gaia', this.processData)
    httpBlock(tv_variety_show, 'tv_variety_show', this.processData)
    httpBlock(tv_hot, 'tv_hot', this.processData)
    httpBlock(book_bestseller, 'book_bestseller', this.processData)
    httpBlock(music_single, 'music_single', this.processData)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    console.log(this.data)
  },

  //点击查看更多
  onTapMore: function (event) {
    const id = event.currentTarget.dataset.id;
    console.log(id)
    wx.navigateTo({
      url: `./more/more?id=${id}`
    })
  },

  //点击查看详情
  onTapDetail: function (event) {
    console.log(event)
    const id = event.currentTarget.dataset.id
    const type = event.currentTarget.dataset.type
    wx.navigateTo({
      url: `./detail/detail?id=${id}&&type=${type}`
    })
  },

  //回调赋值
  processData: function (key, data) {
    this.data.subject[key] = data
    this.setData({
      subject: this.data.subject
    })
  }
})